package 数独检查;

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static int N = 40, n, m;
    static int[][] a = new int[N][N];
    static boolean[] st = new boolean[N];

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        for (int cases = 1; cases <= T; cases++) {
            n = in.nextInt();
            m = n * n;
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < m; j++) {
                    a[i][j] = in.nextInt();
                }
            }
            System.out.printf("Case #%d: ", cases);
            if (check_row() && check_col() && check_cell()) {
                System.out.println("Yes");
            } else {
                System.out.println("No");
            }
        }
    }

    private static boolean check_cell() {
        for (int i = 0; i < m; i += n) {
            for (int j = 0; j < m; j += n) {
                Arrays.fill(st, false);
                for (int dx = 0; dx < n; dx++) {
                    for (int dy = 0; dy < n; dy++) {
                        int t = a[i + dx][j + dy];
                        if (t < 1 || t > m) {
                            return false;
                        }
                        if (st[t]) {
                            return false;
                        }
                        st[t] = true;
                    }
                }
            }
        }
        return true;
    }

    private static boolean check_col() {
        for (int i = 0; i < m; i++) {
            Arrays.fill(st, false);
            for (int j = 0; j < m; j++) {
                int t = a[j][i];
                if (t < 1 || t > m) {
                    return false;
                }
                if (st[t]) {
                    return false;
                }
                st[t] = true;
            }
        }
        return true;
    }

    private static boolean check_row() {
        for (int i = 0; i < m; i++) {
            Arrays.fill(st, false);
            for (int j = 0; j < m; j++) {
                int t = a[i][j];
                if (t < 1 || t > m) {
                    return false;
                }
                if (st[t]) {
                    return false;
                }
                st[t] = true;
            }
        }
        return true;
    }
}
